package com.jph.simple;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.io.ObjectStreamClass;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import adapter.ChatLogAdapter;
import container.FriendManager;
import container.GroupManager;
import model.BaseItem;
import model.Friend;
import model.Group;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    private ListView mRecentChatLv;

    private List<BaseItem> mChatList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_home, container, false);

        initView(v);

        updateChatList();

        ChatLogAdapter chatLogAdapter = new ChatLogAdapter(getActivity(),
                R.layout.chatlog_item, mChatList);
        mRecentChatLv.setAdapter(chatLogAdapter);

        mRecentChatLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {  //点击每一行的点击事件

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                                    long id) {
                BaseItem baseItem= mChatList.get(position);

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString(PlayerChatActivity.ParamName, baseItem.getName());

                String className = baseItem.getClass().getName();
                if (className.equals(Group.class.getName())){
                    intent.setClass(getActivity(), GroupChatActivity.class);
                    getActivity().finish();
                } else if (className.equals(Friend.class.getName())){
                    intent.setClass(getActivity(), PlayerChatActivity.class);
                }

                intent.putExtras(bundle);

                startActivity(intent);

                getActivity().finish();
            }
        });

        return v;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initView(View v){
        mRecentChatLv = (ListView) v.findViewById(R.id.lvRecentChat);
    }

    public void updateUI(){
        updateChatList();
        ((ChatLogAdapter) mRecentChatLv.getAdapter()).notifyDataSetChanged();
    }

    private void updateChatList(){

        // 应该复制出来，不同的ui共用数据，但是排序规则不同
        mChatList.clear();
        mChatList.addAll(FriendManager.getInstance().getFriendList());
        mChatList.addAll(GroupManager.getInstance().getGroupList());

        BaseItem.removeNotChatFriend(mChatList);

        Collections.sort(mChatList, new SortBaseItemByLastChatTime());
    }

}
